// packagePersonalCenter/pages/orderCenter/orderCenter.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    i: '',
    userId: '',
    collectList: '',
    collectList1: '',
    collectList2: '',
    collectList3: '',
    collectList4: '',
    pageNum: 1,

    totalPageCount: 0, //总页数 
    isLoading: false, //节流阀标识
  },
  getData() {
    switch (this.data.i) {
        case '0':
            this.orderInquiry();
            break;
        case '1':
            this.obligationInquiry();
            break;
        case '2':
            this.goodsInquiry();
            break;
        case '3':
            this.completeInquiry();
            break;
        case '4':
            this.cancelInquiry();
            break;
        default:
            break;
    }
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const app = getApp();
    this.setData({
      userId: app.globalData.userInfo.userId,
      baseUrl: app.globalConfig.baseUrl,
    })
  },
  changeIndex(e) {
    this.getData()
    this.setData({
      i: e.target.dataset.i
    })
  },
  goToPay() {
    const quantity = this.data.quantity;
    const productId = this.data.productId;
    const typeId = this.data.typeId;
    const addressId = this.data.addressId;
    const userId = this.data.userId;
    const encodedQuantity = encodeURIComponent(quantity);
    const encodedProductId = encodeURIComponent(productId);
    const encodedTypeId = encodeURIComponent(typeId);
    const encodedAddressId = encodeURIComponent(addressId);
    const encodedUserId = encodeURIComponent(userId);
    const urlWithData = `/packageOnlineStore/pages/confirmOrder/confirmOrder?type=${type}&productNumber=${encodedQuantity}&productId=${encodedProductId}&typeId=${encodedTypeId}&addressId=${encodedAddressId}&userId=${encodedUserId}`;
    wx.navigateTo({
      url: urlWithData
    })
  },
  //全部
  orderInquiry() {
    const token = wx.getStorageSync('token'); //获取令牌
    const baseUrl = this.data.baseUrl //获取本地连接地址
    wx.request({
      url: baseUrl + '/product/productOrder/list',
      method: 'GET',
      data: {
        pageNum: 1,
        pageSize: 6,
        userId: this.data.userId
      },
      header: {
        'authorization': 'Bearer\u0020' + token
      },
      success: (res) => {
        this.setData({
          collectList: res.data.rows
        })
        console.log(this.data.collectList);
      },
    })
  },
  orderInquiry1() {
    const token = wx.getStorageSync('token'); //获取令牌
    const baseUrl = this.data.baseUrl //获取本地连接地址
    //加载时显示loading框
    wx.showLoading({
      title: '数据正在加载',
    })
    wx.request({
      url: baseUrl + '/product/productOrder/list',
      method: 'GET',
      data: {
        pageNum: this.data.pageNum,
        pageSize: 3,
        userId: this.data.userId
      },
      header: {
        'authorization': 'Bearer\u0020' + token
      },
      success: (res) => {
        if (res.data.rows != null) {
          this.setData({
            collectList: this.data.collectList.concat(res.data.rows)
          });
        } else {
          wx.showToast({
            title: '没有更多数据了！',
            icon: 'none'
          })
        }
      },
      complete: (res) => {
        //加载数据完成后关闭loading框
        wx.hideLoading()
        //关闭下拉刷新
        wx.stopPullDownRefresh()
      },
    })
  },
  //待付款
  obligationInquiry() {
    const token = wx.getStorageSync('token'); //获取令牌
    const baseUrl = this.data.baseUrl //获取本地连接地址
    const stateId = "1"
    wx.request({
      url: baseUrl + '/product/productOrder/list',
      method: 'GET',
      data: {
        pageNum: 1,
        pageSize: 3,
        userId: this.data.userId,
        stateId: stateId
      },
      header: {
        'authorization': 'Bearer\u0020' + token
      },
      success: (res) => {
        console.log("成功获取数据" + res.data.rows);
        this.setData({
          collectList1: res.data.rows
        })
        console.log(this.data.collectList1);
      },
    })
  },
  //待收货
  goodsInquiry() {
    const token = wx.getStorageSync('token'); //获取令牌
    const baseUrl = this.data.baseUrl //获取本地连接地址
    const stateId = "2"
    wx.request({
      url: baseUrl + '/product/productOrder/list',
      method: 'GET',
      data: {
        pageNum: 1,
        pageSize: 3,
        userId: this.data.userId,
        stateId: stateId
      },
      header: {
        'authorization': 'Bearer\u0020' + token
      },
      success: (res) => {
        this.setData({
          collectList2: res.data.rows
        })
        console.log(this.data.collectList2);
      },
    })
  },
  //已完成
  completeInquiry() {
    const token = wx.getStorageSync('token'); //获取令牌
    const baseUrl = this.data.baseUrl //获取本地连接地址
    const stateId = "3"
    wx.request({
      url: baseUrl + '/product/productOrder/list',
      method: 'GET',
      data: {
        pageNum: 1,
        pageSize: 3,
        userId: this.data.userId,
        stateId: stateId
      },
      header: {
        'authorization': 'Bearer\u0020' + token
      },
      success: (res) => {
        console.log("成功获取数据" + res.data.rows);
        this.setData({
          collectList3: res.data.rows
        })
        console.log(this.data.collectList3);
      },
    })
  },
  //已取消
  cancelInquiry() {
    const token = wx.getStorageSync('token'); //获取令牌
    const baseUrl = this.data.baseUrl //获取本地连接地址
    const stateId = "4"
    wx.request({
      url: baseUrl + '/product/productOrder/list',
      method: 'GET',
      data: {
        pageNum: 1,
        pageSize: 3,
        userId: this.data.userId,
        stateId: stateId
      },
      header: {
        'authorization': 'Bearer\u0020' + token
      },
      success: (res) => {
        this.setData({
          collectList4: res.data.rows
        })
      },
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.orderInquiry(),
      this.obligationInquiry(),
      this.goodsInquiry(),
      this.completeInquiry(),
      this.cancelInquiry()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    //判断节流阀
    if (this.data.isLoading) {
      return;
    }
    //没有更多数据，提示
    if (this.data.pageNum >= this.data.totalPageCount) {
      wx.showToast({
        title: '没有更多数据了！',
        icon: 'none'
      })
      return;
    }
    //当上提触底的时候，加载当前页+1（下一页）的数据
    this.setData({
      pageNum: this.data.pageNum + 1
    })
    this.orderInquiry1()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})